﻿# needs threshold tuning and multi-faith branch testing before activation.
###################################################
## Regula Temple Service Contract
## laamp_regula_temple_service
##
## Stationary weekly-pulse contract at a temple (or temple citadel) holding.
## Player stays 4-5 weeks, choosing safe or lewd paths each week.
## Lewd success unlocks enhanced rewards (recruits + gold + piety).
## Suspicion from failures can end the contract early.
###################################################

laamp_regula_temple_service = {
	group = laamp_contracts_stewardship_group
	icon = "gfx/interface/icons/task_contracts/regula_icon_task_contract_temple_service.dds"
	travel = no
	use_diplomatic_range = no

	weight = {
		# Standard weights.
		value = task_contract_weight_default_value
		add = laamp_contracts_weight_up_regula_value
		add = laamp_contracts_weight_up_stewardship_value
		# Employer weights.
		scope:employer = {
			add = {
				# Weight up — rulers who care about their temple
				if = {
					limit = { has_trait = zealous }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = generous }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = diligent }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = compassionate }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				# Weight down — rulers who wouldn't bother
				if = {
					limit = { has_trait = cynical }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = greedy }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = lazy }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				# Restrictions.
				min = task_contract_weight_employer_associated_traits_min_value
				max = task_contract_weight_employer_associated_traits_max_value
			}
			multiply = task_contract_weight_by_tier_value
			# This weight is an override, so make sure it's placed at the bottom.
			add = task_contract_weight_employer_contact_list_value
		}
	}

	# Validity Triggers
	valid_to_create = {
		# Game rule gate — respect lewd contract availability setting.
		regula_lewd_contracts_available_trigger = yes
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		employer_has_treasury_to_offer_job_trigger = yes
		valid_laamp_sensible_start_trigger = yes
		# Employer must own a temple (or temple citadel) holding
		scope:employer = {
			regula_temple_service_contract_employer_has_temple_trigger = yes
		}
	}

	valid_to_accept = {
		# Standard triggers.
		valid_laamp_basic_accept_only_trigger = yes
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		is_adult = yes
		OR = {
			carn_is_futa_trigger = yes
			is_male = yes
		}
		is_ai = no
	}

	valid_to_continue = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_employer = { is_landed = yes }
		task_contract_taker = { is_adult = yes is_male = yes is_ai = no }
		var:head_priestess ?= {
			is_alive = yes
		}
		# Employer must still own the temple
		task_contract_employer = {
			regula_temple_service_contract_employer_has_temple_trigger = yes
		}
	}

	valid_to_keep = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_employer = { is_landed = yes }
		task_contract_taker = { is_adult = yes is_male = yes is_ai = no }
		var:head_priestess ?= {
			is_alive = yes
		}
		# Employer must still own the temple
		task_contract_employer = {
			regula_temple_service_contract_employer_has_temple_trigger = yes
		}
	}

	# On_Actions
	on_create = {
		scope:contract = {
			save_scope_as = task_contract

			# Find the temple location from employer's holdings
			task_contract_employer = {
				random_held_title = {
					limit = {
						tier = tier_barony
						title_province = { 
							OR = {
								has_holding_type = church_holding
								has_holding_type = temple_citadel_holding
							}
						}
					}
					title_province = {
						save_scope_as = temple_location
					}
				}
			}

			set_variable = {
				name = temple_location
				value = scope:temple_location
			}

			# Set travel destination to the temple
			set_variable = {
				name = task_contract_destination
				value = scope:temple_location
			}

			# Generate the Head Priestess (first tries to grab a courtier from employer)
			regula_temple_service_contract_generate_head_priestess_effect = yes

			# Make our gold rewards static.
			grab_gold_fuzz_number_effect = yes
			set_variable = {
				name = gold_success_standard
				value = task_contract_taker.task_contract_success_gold_gain_half_value
			}
			save_scope_value_as = { name = gold_safety_margin value = flag:yes }
			set_variable = {
				name = gold_success_critical
				value = task_contract_taker.task_contract_success_gold_gain_full_value
			}
		}
	}

	on_accepted = {
		task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_accept_contract" }
		# Save scopes for the intro event
		save_scope_as = task_contract
		task_contract_employer = { save_scope_as = employer }
		var:head_priestess = { save_scope_as = head_priestess }
		var:temple_location = { save_scope_as = temple_location }

		# Initialize counters
		set_variable = {
			name = lewdness_counter
			value = 0
		}
		set_variable = {
			name = suspicion_counter
			value = 0
		}
		set_variable = {
			name = weeks_elapsed
			value = 0
		}

		# Trigger intro event — check if already at the temple
		if = {
			limit = {
				task_contract_taker = { location = scope:temple_location }
			}
			# Already at the temple — skip travel, go straight to arrival
			task_contract_taker ?= {
				trigger_event = regula_temple_service_contract.0002
			}
		}
		else = {
			# Not at the temple — trigger acceptance event with travel
			task_contract_taker ?= {
				trigger_event = regula_temple_service_contract.0001
			}
		}
	}

	on_invalidated = {
		if = {
			limit = {
				root.task_contract_taker = {
					any_character_active_contract = { this = root }
				}
			}
			# Save scopes for the invalidation event
			task_contract_employer = { save_scope_as = task_contract_employer }
			var:head_priestess = { save_scope_as = head_priestess }

			# Determine reason and trigger letter
			if = {
				limit = {
					task_contract_employer = { is_alive = no }
				}
				task_contract_taker = {
					save_scope_value_as = {
						name = task_contract_invalidation_reason
						value = flag:employer_dead
					}
					trigger_event = regula_temple_service_contract.0041
				}
			}
			else_if = {
				limit = {
					task_contract_employer = { is_landed = no }
				}
				task_contract_taker = {
					save_scope_value_as = {
						name = task_contract_invalidation_reason
						value = flag:employer_lost_land
					}
					trigger_event = regula_temple_service_contract.0041
				}
			}
			else_if = {
				limit = {
					root.var:temple_location = {
						county.holder = {
							NOT = { this = root.task_contract_employer }
						}
					}
				}
				task_contract_taker = {
					save_scope_value_as = {
						name = task_contract_invalidation_reason
						value = flag:employer_lost_temple
					}
					trigger_event = regula_temple_service_contract.0041
				}
			}
			else_if = {
				limit = {
					var:head_priestess = { is_alive = no }
				}
				task_contract_taker = {
					save_scope_value_as = {
						name = task_contract_invalidation_reason
						value = flag:priestess_dead
					}
					trigger_event = regula_temple_service_contract.0041
				}
			}
			# Any other reason — contract ends silently

			# Cleanup generated maidens
			regula_temple_service_contract_cleanup_effect = yes
		}
	}

	# Rewards
	task_contract_reward = {
		success_standard = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				# Camp building extra Stewardship Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_stewardship_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 25
					PROVISIONS = task_contract_success_provisions_gain_major_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
			}
		}
		success_recruitment = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				# Recruit is added via the completion event option
				show_as_tooltip = {
					custom_description = {
						text = regula_female_follower_joins
					}
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 50
					PROVISIONS = task_contract_success_provisions_gain_major_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
			}
		}
		success_critical = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				# All three maidens recruited via the event
				show_as_tooltip = {
					custom_description = {
						text = regula_multiple_female_followers_join
					}
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_full_value
					PIETY = 75
					PROVISIONS = task_contract_success_provisions_gain_massive_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
			}
		}
		failure_standard = {
			positive = no
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_failure_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				# Standard Penalties.
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = 0
					PRESTIGE = task_contract_failure_prestige_loss_full_value
					PIETY = 0
					PROVISIONS = task_contract_failure_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_failure_value
					OPINION_TYPE = failed_task_contract_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
	}
}
